package study.distributed.transaction.config;

import com.alibaba.druid.pool.xa.DruidXADataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

@Configuration
public class Tans0DataSourceConfig {

    @Autowired
    private Trans0Properties trans0Properties;

    @Bean
    @Primary
    public DataSource tans0Datasource() {
        DruidXADataSource druidXADataSource = new DruidXADataSource();
        druidXADataSource.setUrl(trans0Properties.getUrl());
        druidXADataSource.setPassword(trans0Properties.getPassword());
        druidXADataSource.setUsername(trans0Properties.getUsername());
        druidXADataSource.setDriverClassName(trans0Properties.getDriverClassName());
        //再者就是要把是MysqlXADataSource设置到AtomikosDataSourceBean中
        AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
        xaDataSource.setXaDataSource(druidXADataSource);
        xaDataSource.setUniqueResourceName("tans0Datasource");
        return xaDataSource;
    }

    @Bean
    @Primary
    public SqlSessionFactory tans0SqlSessionFactory(@Qualifier(value = "tans0Datasource")  DataSource dataSource, @Qualifier("trans0Configuration") org.apache.ibatis.session.Configuration configuration) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setConfiguration(configuration);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/transaction0/*.xml"));
        return factoryBean.getObject();
    }

    @Bean
    public org.apache.ibatis.session.Configuration trans0Configuration() {
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        return configuration;
    }

    @Configuration
    @AutoConfigureAfter(Tans0DataSourceConfig.class)
    public static class MybatisMapperConfiguration{

        @Bean
        public MapperScannerConfigurer tans0MapperScannerConfigurer() {
            MapperScannerConfigurer configurer = new MapperScannerConfigurer();
            configurer.setSqlSessionFactoryBeanName("tans0SqlSessionFactory");
            configurer.setBasePackage("study.distributed.transaction.dao.transaction0");
            return configurer;
        }
    }


}
